/*
 * ConstraintViolation.java
 * 
 * Created on 21-Aug-2007, 07:54:07
 *
 * Copyright (C)
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

package com.grubby.databean.constraints.validation;

import java.lang.annotation.Annotation;

/**
 * Represents a violation of a constraint. Captures the constraint that was
 * violated and a message describing the violation.
 * <p>
 * Note there may be violations of several constraints during the validation of
 * a property. All these violations are captured as separate ConstraintViolation
 * objects and typically rolled up as part of an overall validation result.
 * </p>
 * <p>
 * Note this has been adapted from an Annotation processing example created by 
 * Anders Holmgren of Sun Microsystems
 * </p>
 * @see PropertyValidationErrors
 *
 * @author Anders Holmgren, with alterations by A. HARMEL-LAW. 
 * E: andrew.harmel.law@gmail.com
 * @version 0.1
 */
public class ConstraintViolation {
    
    private Annotation constraint;
    private String message;

    /**
     * Creates a ConstraintViolation with the violated constraint and 
     * message describing the violation
     * @throws IllegalArgumentException if constraint is null or message is null
     */
    public ConstraintViolation(Annotation constraint, String message) {
        if (constraint == null)
            throw new IllegalArgumentException("null is not a legal value for " 
                    + "constraint");
        if (message == null)
            throw new IllegalArgumentException("null is not a legal value for "
                    + " message");
        
        this.constraint = constraint;
        this.message = message;
    }

    /**
     * The constraint that was violated
     */
    public Annotation getConstraint() {
        return constraint;
    }

    /**
     * Text describing the violation. Note applications do not necessarily need
     * to make use of this message, as they have enough information to create
     * their own messages. In particular they know the constraint that was 
     * violated and the value that violated the constraint. As such this message
     * can be considered a default message and is useful for logging etc.
     */
    public String getMessage() {
        return message;
    }
}